import {getSetting} from "@/api";
import {titleControll} from "@/utils";

export default {
    namespaced:true,//命名空间
    state:{
        loading:false,
        data:null
    },
    mutations:{
        setLoading(state,payload){
            state.loading = payload;
        },
        setData(state,payload){
            state.data = payload;
        }
    },
    actions:{
        async fetchSetting(ctx){
            ctx.commit('setLoading',true);
            const setting = await getSetting();
            ctx.commit('setData',setting);
            ctx.commit('setLoading',false);

            //设置页面图标
            if(setting.favicon){
                let link = document.querySelector("link[ref='shortcut icon'");
                if(link){
                    return;
                }
                link = document.createElement('link');
                link.rel="shortcut icon";
                link.type="images/x-icon";
                link.href=setting.favicon;
                document.querySelector('head').appendChild(link);
            }

            //设置网站标题
            if(setting.siteTitle){
                titleControll.setSiteTitle(setting.siteTitle);
            }
        }
    }
}